package leecode

import "sort"

func minOperationsToMakeMedianK(nums []int, k int) int64 {
	sort.Ints(nums)
	minNum := nums[len(nums)/2]
	var result int
	if minNum > k {
		for i := len(nums) / 2; i >= 0 && nums[i] > k; i-- {
			result += nums[i] - k
		}
	} else if minNum < k {
		for i := len(nums) / 2; i < len(nums) && k > nums[i]; i++ {
			result += k - nums[i]
		}
	}
	return int64(result)
}
